<?php

class Tutorial {
    private $filename;
    private $document;
    private $toc;
    private $children;
    private $current;
    
    public function __construct($filename) {
        
        if(preg_match('/^[a-zA-z0-9]+$/',$filename)) {
            $file = Settings::getHome().'/tutoriales/'.$filename.'.xhtml';
        }
        else {
            throw new Exception('Invalid filename: '.$filename);
        }
        
        if(file_exists($file)) {
            if($document = DOMDocument::loadHTMLFile($file)) {
                $this->filename = $filename;
                $this->document = $document;
            }
            else {
                throw new Exception('File could not be loaded as DOM: '.$file);
            }
        }
        else {
            throw new Exception('File does not exist: '.$file);
        }
    }
    
    public function getChildren() {
        if(isset($this->children))
            return $this->children;
            
        $children = array();
        if($list = $this->document->getElementById('toc')) {
            foreach($list->getElementsByTagName('a') as $a) { 
                $children[$a->getAttribute('href')] = new Tutorial($a->getAttribute('href'));
            }
        }
        
        $this->children = $children;
        return $this->children;
    }
    
    private function hasChild($name) {
        $children = $this->getChildren();
        if(array_key_exists($name, $children)) {
            return true;
        }
        foreach($children as $child) {
            if($child->hasChild($name))
                return true;
        }
        return false;
    }
    
    public function moveTo($name) {
        if($this->hasChild($name)) {
            $this->current = $name;
        }
        else {
            throw new Exception($name.' is not a child of '.$this->getTitle());
        }
    }
    
    private function appendSubTOC($document, $toc, $rootTutorial, $current, $level) {        
        $children = $this->getChildren();
        foreach($children as $child) {
            $div=$document->createElement('div');
            if($current == $child->getFileName())
                $div->setAttribute('class', 'current tocitem level'.$level);
            else
                $div->setAttribute('class', 'tocitem level'.$level);
            $toc->appendChild($div);
            
            $a = $document->createElement('a', $child->getTitle());
            $a->setAttribute('href', '/tutoriales/'.$rootTutorial.'/'.$child->getFileName());
            $div->appendChild($a);
            
            if($child->getChildren()) {
                $child->appendSubTOC($document, $toc, $rootTutorial, $current, $level+1);
            }
            
        }
        return $div;
    }
    
    public function getTOC() {
        if(isset($this->toc))
            return $this->toc;
            
        if($children = $this->getChildren()) {
            $document = new DOMDocument('1.1', 'UTF-8');
            $document->formatOutput = true;
            
            //Create div to hold TOC
            $toc = $document->createElement('div');
            $toc->setAttribute('class', 'toc');
            $document->appendChild($toc);
            
            //Append link to root tutorial
            $title = $document->createElement('div');
            if(!isset($this->current))
                $title->setAttribute('class', 'toctitle current');
            else
                $title->setAttribute('class', 'toctitle');
            $toc->appendChild($title);
            
            $a = $document->createElement('a', $this->getTitle());
            $a->setAttribute('href', '/tutoriales/'.$this->getFileName().'/');
            $title->appendChild($a);
            
            //Append links for children
            $rootTutorial = $this->getFileName();
            if(isset($this->current))
                $currentTutorial = $this->current;
            else
                $currentTutorial = $this->getFileName();
            $this->appendSubTOC($document, $toc, $rootTutorial, $currentTutorial, 1);
            
            $this->toc = $document->saveXML($toc);
            return $this->toc;
        }
        else {
            $this->toc = '';
            return $this->toc;
        }
        
    }    
    
    public function getFileName() {
        return $this->filename;
    }
    
    public function getTitle() {
        if($title=$this->document->getElementsByTagName('title')->item(0))
            return $title->nodeValue;
        else
            return $this->getFileName();
    }
    
    public function getContent() {
        if($content=$this->document->getElementById('tutorial')) {
            $dom = new DOMDocument('1.1', 'UTF-8');
            $dom->formatOutput = true;
            $dom->appendChild($dom->importNode($content,true));
            return $dom->saveXML($dom->getElementById('tutorial'));
        }
        else
            return '';
    }
    
    public function getCurrentTitle() {
        if(isset($this->current)) {
            $current = new Tutorial($this->current);
            return $current->getTitle();
        }
        else
            return $this->getTitle();
    }
    
    public function getCurrentContent() {
        if(isset($this->current)) {
            $current = new Tutorial($this->current);
            return $current->getContent();
        }
        else {
            return $this->getContent();
        }
    }
}
?>
